package com.ff.device;

import com.ff.device.util.CmdUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Algorithm {

    private static final Logger logger = LoggerFactory
            .getLogger(Algorithm.class);

    public static int[] arr=new int[]{10,2,1,36,7,53,9,6,90,500,30,20,10000,88};

    public static void maopao_sort(){
        for(int i=0;i<arr.length-1;i++)
            for (int j=0;j<arr.length-i-1;j++){
                if (arr[j]>arr[j+1]){
                    int tmp=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=tmp;
                }
            }
        for(int i=0;i<arr.length;i++)
            logger.info(arr[i]+"");
    }

    public static void choice_sort(){
        for (int i=0;i<arr.length-1;i++){
            int cur=arr[i];
            int index=i;
            for (int j=i+1;j<arr.length;j++){
                if (cur>arr[j]){
                    cur=arr[j];
                    index=j;
                }
            }
            if (index!=i){
                int tmp=arr[i];
                arr[i]=arr[index];
                arr[index]=tmp;
            }
        }
//        for(int i=0;i<arr.length;i++)
//            logger.info(arr[i]+"");
    }

    public static int erfenchazhao(int a){
        choice_sort();
        int left=0;
        int right=arr.length-1;
        int mid;
        while (left<right){
            mid=(left+right)/2;
            if (a==arr[mid]){
                return mid;
            }else if(a>arr[mid]){
                left=mid+1;
            }else{
                right=mid-1;
            }
        }
        return -1;
    }

    public static void sort(int arr[],int left,int right){
        if (left>=right)
            return;
        int l=left;
        int r=right;
        int povit=arr[left];
        while (l<r){
            while (l<r&&arr[r]>=povit){
                r--;
            }
            if (l<r){
                arr[l]=arr[r];
            }

            while(l<r&&arr[l]<=povit)
                l++;
            if (l<r){
                arr[r]=arr[l];
            }
        }
        arr[l]=povit;

        sort(arr,left,l-1);
        sort(arr,l+1,right);
    }

    public static void main(String[] args) {
//        maopao_sort();
//        logger.info(erfenchazhao(1)+"");
        sort(arr,0,arr.length-1);
        for(int i=0;i<arr.length;i++)
            logger.info(arr[i]+"");
    }
}
